#!/usr/bin/env python

try:
	import pdb
	import pygame
	import sys
	from data.scenarios import *
	from langbattlefield import *
	from langinfobar import *
	#~ from command import Command
	#~ from creature import Creature
	from uicursor import *
	from uipanel import *
	from pygame.locals import *
	#~ from utils import *
except ImportError, err:
	print "Could not load %s module." % (err)
	raise SystemExit


class ScenarioEngine(object):
	def __init__(self):
		object.__init__(self)

		#~ self.clock = clock
		self.screen = pygame.display.get_surface()
		#~ self.scen_data = ScenarioDef()
		self.animating = False

		self.field = Battlefield(scenarios[18].file)
		self.info_bar = InfoBar(self.field)

		# init layers (map, creatures, other, cursor)
		self.layers = []
		self.layers.append(pygame.sprite.RenderPlain(self.field))
		self.layers.append(pygame.sprite.RenderPlain(self.info_bar))
		self.layers.append(pygame.sprite.RenderPlain())
		self.layers.append(pygame.sprite.RenderPlain())

		
		cursor = Cursor(self.layers[2])

		self.inputstack = []
		self.inputstack.append(cursor)

		#sets cursor for the info bar and updates it
		self.info_bar.set_cursor(cursor)
		self.layers[1].update()
		
		#~ self.command = None
		#~ self.window = None list
		#self.sys_menu = None

	def draw_layers(self):
		self.screen.fill((0,0,0)) # to fill the screen if nothing drawn
		[[l.update(), l.draw(self.screen)] for l in self.layers]

	def do_cancel(self):
		self.inputstack[-1].close()
		return

	def finish_window_close(self):
		# called on USEREVENT WINDOW_CLOSED
		self.inputstack.pop()
		self.inputstack[-1].show()
		return

	def do_move(self, key, mod):
		self.inputstack[-1].move(key, mod) or (
			[spr.r_move(key, mod) for spr in self.layers[0]] and
			[spr.r_move(key, mod) for spr in self.layers[1]]
		)

	def do_action(self):
		self.inputstack[-1].do_action(self)
		return

	def load_sys_menu(self):
		self.inputstack.append(SystemMenu(self.layers))
		self.inputstack[-1].open()
#		if self.sys_menu is None:
#			self.sys_menu = SystemMenu(self.layers)
#		self.sys_menu.open()
		

	def is_animating(self):
		if self.inputstack[-1].animating == [] or [[],[]]:
			return False
		return self.inputstack[-1].animating
	